var timeFactory = require("~/user-module/timeFactory.js").timeFactory;
timeFactory = new timeFactory();

exports.weatherFactory = function(params) {
    this.getweatherbyip = co(function* (params) {
        var temp = yield http.getJSON({
            url: "http://route.showapi.com/9-4?showapi_timestamp=" + timeFactory.getTimeStamp() + "&showapi_appid=5653&showapi_sign=f668fa7e626043b19b34a61743fcf271&needMoreDay=1&needIndex=1&needIndex=1&need3HourForcast=1",
            method: "GET",
            headers: {
                "Content-Type": "application/json"
            }
        });
        return temp.showapi_res_body;
        // .then(function(temp) {
        //     console.log(JSON.stringify(temp));
        //     resolve(temp);

        //     console.log(weatherMap[weatherInfo.now.weather])
        //     // console.log(weatherInfo.f1.day_air_temperature);
        // });
    }).catch(e => {
        console.log(e);
    });
    this.getweatherbycityname = name => {
        return co(function* () {
            var temp = yield http.getJSON({
                url: "http://route.showapi.com/9-2?area=" + name + "&showapi_timestamp=" + timeFactory.getTimeStamp() + "&showapi_appid=5653&showapi_sign=f668fa7e626043b19b34a61743fcf271&needMoreDay=1&needIndex=1&needIndex=1&need3HourForcast=1",
                method: "GET",
                headers: {
                    "Content-Type": "application/json"
                }
            });
            return temp.showapi_res_body;
        }).catch(e => {
            console.log(e);
        });
    };
    this.getweatherbylocation = (lat, lng) => {
        return co(function* () {
            var temp = yield http.getJSON({
                url: "http://route.showapi.com/9-5?from=5&lng=" + lng + "&lat=" + lat + "&showapi_timestamp=" + timeFactory.getTimeStamp() + "&showapi_appid=5653&showapi_sign=f668fa7e626043b19b34a61743fcf271&needMoreDay=1&needIndex=1&needIndex=1&need3HourForcast=1",
                method: "GET",
                headers: {
                    "Content-Type": "application/json"
                }
            });
            return temp.showapi_res_body;
        }).catch(e => {
            console.log(e);
        });
    };
};